/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//列表模块
var List = (function (Item) {

    //data
    var _list = new Array();

    //加载此模块产出html的容器
    var _container;

    //设置容器,核心方法
    var bind = function ($element) {
        _container = $element;
    }

    //呈现，根据list的数据，将模板render出来,核心方法
    var _render = function () {

        var data = {
            'list': _list
        };

        if (_container != null) {
            _container.html(template('list', data));
        } else {
            alert('you should bind the container first');
        }
    };

    //list，业务逻辑
    var list = function () {
        var item = Object.create(Item);
        item.id = 1;
        item.project = 'sample1';
        item.manager = 'nancy';
        item.state = 1;
        //add item to array
        _list.push(item);

        var item = Object.create(Item);
        item.id = 2;
        item.project = 'sample2';
        item.manager = 'nancy';
        item.state = 2;
        //add item to array
        _list.push(item);

        //render
        _render();
    }

    //add，业务逻辑
    var add = function () {
        //create item
        var item = Object.create(Item);
        item.id = 3;
        item.project = 'sample3';
        item.manager = 'nancy';
        item.state = 2;
        //add item to array
        _list.push(item);

        //render
        _render();
    }

    //del，业务逻辑
    var del = function () {
        _list.pop();

        //render
        _render();
    }

    return {
        bind: bind,
        list: list,
        add: add,
        del: del
    };
})(Item);


